﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace HOBA.SMSSender
{
    public partial class DeliveryReports : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int totalpage = GetTotalPage();
            GeneratePageNavigation(totalpage);

            string currentpage = "";
            if (Request.QueryString.Count == 0 || Int32.Parse(Request.QueryString["p"]) < 0
                || Int32.Parse(Request.QueryString["p"]) > totalpage)
                currentpage = "1";
            else
                currentpage = Request.QueryString["p"];

            //TODO: get page content
            //string filePath = GetPageContent(currentpage);
            string filePath = Server.MapPath("~/temp_reports.xml");
            AddMessages(filePath);
        }

        private int GetTotalPage()
        {
            try
            {
                MySoapService service = new MySoapService("http://center.fibosms.com/Service.asmx");
                string response = service.GetTotalPageOfClientSMS(Session["username"].ToString(), Session["password"].ToString(), "hoba");
                return Int32.Parse(FileUtility.ReadTotalPage(response));
            }
            catch (Exception exc)
            {
                return 0;
            }
        }

        private void GeneratePageNavigation(int totalPage)
        {
            string html = "";
            string btnPage = "";
            for (int i = 1; i <= totalPage; i++)
            {
                btnPage = "<a href='DeliveryReports.aspx?p=" + i.ToString() + "'>" + i.ToString() + "</a>&nbsp;&nbsp;";
                html += btnPage;
            }
            pages.InnerHtml = html;
        }

        private void GetPageContent(string currentpage)
        {
            try
            {
                MySoapService service = new MySoapService("http://center.fibosms.com/Service.asmx");
                string response = service.GetClientSMSListOfPage(Session["username"].ToString(), Session["password"].ToString()
                    , currentpage, new DateTime(2001, 1, 1), DateTime.Now, "0,1,2,3,4,5,6,7,8,9,10,11", "1357", "hoba");
                AddMessages(response);
            }
            catch (Exception exc)
            {
            }
        }

        private void AddMessages(string xmlContent)
        {
            XmlDocument list = new XmlDocument();
            list.LoadXml(xmlContent);

            // Node for each sms message in response file
            XmlNodeList xmlmessages = list.GetElementsByTagName("SMSLIST");
            // A whole list of messages
            string divMessages = "";
            // Info of individual message
            string divMessage = "";
            if (xmlmessages != null)
            {
                for (int i = 0; i < xmlmessages.Count; i++)
                {
                    XmlNode thismessage = xmlmessages[i];

                    divMessage = "<ul class='";
                    if ((i + 1) % 2 == 0)
                        divMessage += "celleven'";
                    else
                        divMessage += "cellodd'";

                    divMessage += " onclick='showThis(this);'>";

                    divMessage += "<li>unknown</li>"; // Sender's phone number
                    divMessage += "<li>" + thismessage["PhoneNumber"].InnerText + "</li>"; // Recipient's phone number
                    divMessage += "<li>" + thismessage["Message"].InnerText + "</li>";
                    divMessage += "<li>" + thismessage["CreatedDate"].InnerText + "</li>";
                    divMessage += "<li>" + thismessage["SentDate"].InnerText + "</li>";
                    divMessage += "<li>";

                    switch (thismessage["SMSStatus"].InnerText)
                    {
                        case "2":
                            divMessage += "Sent";
                            break;
                        case "0":
                            divMessage += "Pending";
                            break;
                        case "1":
                            divMessage += "Sending";
                            break;
                        case "3":
                            divMessage += "Failed";
                            break;
                        case "4":
                            divMessage += "Deleted";
                            break;
                        case "5":
                            divMessage += "Wrong recipient number";
                            break;
                        case "6":
                            divMessage += "Cannot send";
                            break;
                        case "7":
                            divMessage += "Out of date";
                            break;
                        case "8":
                            divMessage += "Send MT failed";
                            break;
                        case "9":
                            divMessage += "Other";
                            break;
                        case "10":
                            divMessage += "SpamSMS";
                            break;
                        case "11":
                            divMessage += "Restored to SMS Client";
                            break;
                    }
                    divMessage += "</li></ul>";
                    divMessages += divMessage;
                }
                messages.InnerHtml = divMessages;
            }
        }
    }
}
